package week3;

import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo84 {
    @Test
    public void t1() throws SquareRoot {
        Scanner scanner = new Scanner(System.in);
        double num = scanner.nextDouble();
        if (num >= 0){
            System.out.println("输出平方根");
        } else {
            throw new SquareRoot(num);
        }
    }

/*    @Test
    public void t2(){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1、加载驱动Class.forName("");
            Class.forName("com.mysql.jdbc.Driver");
            // 2、获得连接对象Connection,问号前面是自己的数据库
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1812?useUnicode=true&characterEncoding=UTF-8", "root", "331226");
            // 3、写sql语句
            String sql = "SELECT id,`name`,age,gender FROM student";
            // 使用预编译
            statement = connection.prepareStatement(sql);
            // 5、执行sql语句
            //         (1) 更新类（更改了表里面数据）：delete/update/insert         executeUpdate()
            // 返回值：int，表示你影响的行数
            //         (2)查询（没有改变表里面数据）:  select                       executeQuery()
            // 返回值：结果集ResultSet
            resultSet = statement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            // next()方法判断下一行有没有，如果有指向下一行
            while (resultSet.next()) {
                // 每遍历一次循环，就拿出数据库查询结果里面的一行记录，就是一个学生的信息,
                // 就可以封装成一个Student对象
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
            // 6、关闭连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //先打开后关闭

            }
        }
    }*/

    @Test
    public void testDelete(){//使用util工具类，方便加载驱动，或得连接对象，关闭连接
        Connection connection = null ;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "DELETE FROM student where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,1);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JavaUtil.close(connection, statement ,null);
        }
    }


    //基本数据类型int
    @Test
    public void t11(){
        int num = 10;
        num = 20;
        foo(num);//num实参
        System.out.println(num);//输出20
    }
    void foo(int value){//value是形参，value = num（假设这个value写成num，值仍不变num = num）
        value = 100;
    }

    //String类型
    @Test
    public void t22(){
        String str = "hello";
        str = "java";
        fii(str);//str实参
        System.out.println(str);//输出java
    }
    void fii(String text){//text形参 text = str
        text = "windows";
    }

    public static void main(String[] args) {
        System.out.println(test());

    }
    private static int test() {
        int temp = 1;
        try {
            System.out.println(temp);
            return ++temp;
        } catch (Exception e) {
            System.out.println(temp);
            return ++temp;
        } finally {
            ++temp;
            System.out.println(temp);
        }
    }


    static boolean foo(char c)
    {
        System.out.print(c);
        return true;
    }

    @Test
    public void t99() {
        int i =0;
        for(foo('A');foo('B')&&(i<2);foo('C'))
        {
            i++;
            foo('D');
        }
    }
}
